package binarycrew.livewallpapers.utils;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;
import binarycrew.livewallpapers.utils.WallpapersProvider.DownloadedGalleries;
import binarycrew.livewallpapers.utils.WallpapersProvider.TimeColumns;

/**
 * Helper for managing {@link SQLiteDatabase} that stores data for {@link ScheduleProvider}.
 */
public class WallpapersDatabase extends SQLiteOpenHelper {

	private static final String TAG = "WallpapersDatabase";

	public static final String DATABASE_NAME = "wallpaper.db";

	private static final int DATABASE_VERSION = 1;

	public interface Tables {

		String DOWNLOADED_GALLERIES = "downloaded_galleries";
		String PHOTO_SOURCES = "photo_sources";
	}

	private static WallpapersDatabase _instance;

	private WallpapersDatabase(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	public static synchronized WallpapersDatabase getInstance(Context context) {
		if (_instance == null) {
			_instance = new WallpapersDatabase(context);
		}
		return _instance;
	}

	@Override
	public void close() {
		super.close();
		_instance = null;
	}

	@Override
	public void onOpen(SQLiteDatabase db) {
		//Enable foreign key constraints in for sqlite3. Turned off by default
		db.execSQL("PRAGMA foreign_keys = ON");
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		//Foreign key support works with sqlite version 3.6.19 an above (Android 2.2)
		Log.d("CREATE DB", db.getPath());

		//		db.execSQL("CREATE TABLE " + Tables.PHOTO_SOURCES + " ("
		//				+ BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
		//				+ PhotoSource.COPYRIGHT + "varchar(63),"
		//				+ PhotoSource.URL + " TEXT NOT NULL)"
		//				);

		db.execSQL("CREATE TABLE " + Tables.DOWNLOADED_GALLERIES + " ("
				+ BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
				+ TimeColumns.TIMESTAMP + " INTEGER NOT NULL,"
				+ TimeColumns.TIME + " varchar(63), " //human readable
				//				+ DownloadedGalleries.PHOTO_SOURCE_ID + "INTEGER NOT NULL,"
				+ DownloadedGalleries.URL + " TEXT NOT NULL)"
				//				+ ", FOREIGN KEY(" + DownloadedGalleries.PHOTO_SOURCE_ID + ") REFERENCES " + Tables.PHOTO_SOURCES
				//				+ "(" + PhotoSource._ID + ") )"
				);

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.d(TAG, "onUpgrade() from " + oldVersion + " to " + newVersion);

	}

	public boolean containsGallery(String gallery) {
		Cursor cursor =
				getReadableDatabase().query(Tables.DOWNLOADED_GALLERIES, null, DownloadedGalleries.URL + " = ?",
						new String[] { gallery }, null, null, null);
		int count = cursor.getCount();
		cursor.close();
		Log.d(TAG, "constains " + gallery + " : " + (count == 1));
		return (count == 1);
	}

	public void insertGallery(String detailUrl) {
		Log.d(TAG, "insert: " + detailUrl);
		getWritableDatabase().insert(Tables.DOWNLOADED_GALLERIES, null,
				DownloadedGalleries.buildValues(System.currentTimeMillis(), detailUrl));

	}

}
